package com.guldan.jaina.manager;

import com.guldan.core.constants.CommonConstant;
import com.guldan.core.enums.ProjectRegisterBatchEn;
import com.guldan.core.enums.ProjectRegisterEn;
import com.guldan.core.manager.BaseManager;
import com.guldan.core.search.Pagination;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.area.AreaDTO;
import com.guldan.jaina.dto.citizenbank.CitizenBankDTO;
import com.guldan.jaina.dto.citizeninfo.CitizenInfoDTO;
import com.guldan.jaina.dto.citizeninfo.CitizenInfoSearchDTO;
import com.guldan.jaina.dto.dictionary.DictionaryDTO;
import com.guldan.jaina.dto.orgbatchsummary.OrgBatchSummaryDTO;
import com.guldan.jaina.dto.paysummary.PaySummaryDTO;
import com.guldan.jaina.dto.progress.BarDTO;
import com.guldan.jaina.dto.projectextensiondetail.ProjectExtensionDetailDTO;
import com.guldan.jaina.dto.projectregisterupload.ProjectRegisterUploadDTO;
import com.guldan.jaina.dto.register.RegisterTemplateDTO;
import com.guldan.jaina.dto.register.RegisterTemplateSearchDTO;
import com.guldan.jaina.dto.registersuccess.RegisterDeleteDTO;
import com.guldan.jaina.dto.registersuccess.RegisterSuccessDTO;
import com.guldan.jaina.dto.registersuccess.RegisterSuccessSearchDTO;
import com.guldan.jaina.utils.CitizenInfoDomainConvertUtils;
import com.guldan.jaina.utils.ProjectRegisterUploadDomainConvertUtils;
import com.guldan.jaina.utils.RegisterSuccessDomainConvertUtils;
import com.guldan.jaina.utils.UserUtils;
import com.guldan.mapper.db.entity.*;
import com.guldan.mapper.db.entity.Dictionary;
import com.guldan.mapper.db.mapper.AreaMapperExt;
import com.guldan.mapper.db.mapper.ProjectMapperExt;
import com.guldan.mapper.db.mapper.ProjectRegisterBatchMapperExt;
import com.guldan.mapper.db.mapper.RegisterSuccessMapperExt;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Silas.
 * Date: 2016/12/3
 * Time: 17:36
 */
@Service
public class RegisterSuccessManager extends BaseManager {

    @Autowired
    private RegisterSuccessMapperExt registerSuccessMapperExt;

    @Autowired
    private ProjectExtensionDetailManager projectExtensionDetailManager;

    @Autowired
    private AreaMapperExt areaMapperExt;

    @Autowired
    private ProjectMapperExt projectMapperExt;

    @Autowired
    private ProjectRegisterBatchMapperExt projectRegisterBatchMapperExt;

    @Autowired
    private ProjectManager projectManager;

    @Autowired
    private OrgBatchSummaryManager orgBatchSummaryManager;

    @Autowired
    private PaySummaryManager paySummaryManager;

    //列表查看所有失败人员的列表
    public Pagination<RegisterSuccessDTO> failList(RegisterSuccessSearchDTO searchDTO) {

        List<Integer> state = new ArrayList<>();
        state.add(ProjectRegisterEn.auditor_fail.getCode());
        state.add(ProjectRegisterEn.org_auditor_fail.getCode());
        state.add(ProjectRegisterEn.finance_auditor_fail.getCode());
        state.add(ProjectRegisterEn.bank_fail.getCode());
        searchDTO.setState(state);

        return listCitizen(searchDTO);
    }

    //列表查看所有成功人员的列表
    public Pagination<RegisterSuccessDTO> successList(RegisterSuccessSearchDTO searchDTO) {

        List<Integer> state = new ArrayList<>();
        state.add(ProjectRegisterEn.register_ing.getCode());
        state.add(ProjectRegisterEn.auditor_ing.getCode());
        state.add(ProjectRegisterEn.auditor_ed.getCode());
        state.add(ProjectRegisterEn.org_auditor_ing.getCode());
        state.add(ProjectRegisterEn.org_auditor_ed.getCode());
        state.add(ProjectRegisterEn.finance_auditor_ing.getCode());
        state.add(ProjectRegisterEn.finance_auditor_ed.getCode());
        state.add(ProjectRegisterEn.finance_created_file.getCode());
        state.add(ProjectRegisterEn.finance_downloaded_file.getCode());
        state.add(ProjectRegisterEn.bank_success.getCode());
        searchDTO.setState(state);

        return listCitizen(searchDTO);
    }

    /**
     * 保存RegisterSuccess
     *
     * @param dto
     * @return
     */
    public RegisterSuccessDTO save(RegisterSuccessDTO dto) {
        RegisterSuccess entity;
        if (dto.getRegisterId() != null && dto.getRegisterId() > 0) {
            entity = registerSuccessMapperExt.selectByPrimaryKey(dto.getRegisterId());
            entity.setAddress(dto.getAddress());
            entity.setAddressZw(dto.getAddressZw());
            entity.setArea(dto.getArea());
            entity.setAreaId(dto.getAreaId());
            entity.setBankCode(dto.getBankCode());
            entity.setBankNumber(dto.getBankNumber());
            entity.setBankPayDate(DateUtil.parse(dto.getBankPayDate(), DateUtil.DAY_FORMAT));
            entity.setBatchId(dto.getBatchId());
            entity.setBirthday(DateUtil.parse(dto.getBirthday(), DateUtil.DEFAULT_DATE_FORMAT));
            entity.setCitizenInfoId(dto.getCitizenInfoId());
            entity.setCitizenInfoTypeCode(dto.getCitizenInfoTypeCode());
            entity.setDegreeCode(dto.getDegreeCode());
            entity.setDeliverBankDate(DateUtil.parse(dto.getDeliverBankDate(), DateUtil.DEFAULT_DATE_FORMAT));
            entity.setBankPayDate(DateUtil.parse(dto.getBankPayDate(), DateUtil.DAY_FORMAT));
            entity.setFailReason(dto.getFailReason());
            entity.setFinanceAuditorId(dto.getFinanceAuditorId());
            entity.setFinanceAuditTime(DateUtil.parse(dto.getFinanceAuditorId(), DateUtil.DEFAULT_DATE_FORMAT));
            entity.setFinancePayerId(dto.getFinancePayerId());
            entity.setGenderCode(dto.getGenderCode());
            entity.setHouseholdId(dto.getHouseholdId());
            entity.setHouseholdRegisterNumber(dto.getHouseholdRegisterNumber());
            entity.setHouseHoldTypeCode(dto.getHouseHoldTypeCode());
            entity.setIdentNumber(dto.getIdentNumber());
            entity.setIdentTypeCode(dto.getIdentTypeCode());
            entity.setIsMaster(dto.getMaster());
            entity.setName(dto.getName());
            entity.setNameZw(dto.getNameZw());
            entity.setNationCode(dto.getNationCode());
            entity.setOrgAuditorId(entity.getOrgAuditorId());
            entity.setOrgAuditTime(DateUtil.parse(dto.getOrgAuditTime(), DateUtil.DEFAULT_DATE_FORMAT));
            entity.setOrgCode(dto.getOrgCode());
            entity.setPayCycleCode(dto.getPayCycleCode());
            entity.setPayStandardAmt(dto.getPayStandardAmt());
            entity.setProjectId(dto.getProjectId());
            entity.setRelationCode(dto.getRelationCode());
            entity.setState(dto.getState());
            entity.setSubsidyCount(dto.getSubsidyCount());
            entity.setSubsidyMoney(dto.getSubsidyMoney());
            entity.setSubsidyNum(dto.getSubsidyNum());
            entity.setSummaryId(dto.getSummaryId());
            entity.setTelephone(dto.getTelephone());
            entity.setTotalFormula(dto.getTotalFormula());
            entity.setTownAuditor(dto.getTownAuditor());
            entity.setTownAuditTime(DateUtil.parse(dto.getTownAuditTime(), DateUtil.DEFAULT_DATE_FORMAT));
            entity.setTownRegisterTime(DateUtil.parse(dto.getTownRegisterTime(), DateUtil.DEFAULT_DATE_FORMAT));
            entity.setTownReigsterId(dto.getTownReigsterId());
            entity.setUploadId(dto.getUploadId());
            entity.setYear(dto.getYear());
            entity.setAge(dto.getAge());

            entity.setDisabled(dto.getDisabled());
            entity.setModifyTime(DateUtil.getCurrentTime());
            if (UserUtils.getUser() != null) {
                entity.setModifier(UserUtils.getUser().getUserName());
            }
            registerSuccessMapperExt.updateByPrimaryKeySelective(entity);
        } else {
            entity = new RegisterSuccess();
            entity.setAddress(dto.getAddress());
            entity.setAddressZw(dto.getAddressZw());
            entity.setArea(dto.getArea());
            entity.setAreaId(dto.getAreaId());
            entity.setBankCode(dto.getBankCode());
            entity.setBankNumber(dto.getBankNumber());
            entity.setBankPayDate(DateUtil.parse(dto.getBankPayDate(), DateUtil.DAY_FORMAT));
            entity.setBatchId(dto.getBatchId());
            entity.setBirthday(DateUtil.parse(dto.getBirthday(), DateUtil.DEFAULT_DATE_FORMAT));
            entity.setCitizenInfoId(dto.getCitizenInfoId());
            entity.setCitizenInfoTypeCode(dto.getCitizenInfoTypeCode());
            entity.setDegreeCode(dto.getDegreeCode());
            entity.setDeliverBankDate(DateUtil.parse(dto.getDeliverBankDate(), DateUtil.DEFAULT_DATE_FORMAT));
            entity.setFailReason(dto.getFailReason());
            entity.setFinanceAuditorId(dto.getFinanceAuditorId());
            entity.setFinanceAuditTime(DateUtil.parse(dto.getFinanceAuditorId(), DateUtil.DEFAULT_DATE_FORMAT));
            entity.setFinancePayerId(dto.getFinancePayerId());
            entity.setGenderCode(dto.getGenderCode());
            entity.setHouseholdId(dto.getHouseholdId());
            entity.setHouseholdRegisterNumber(dto.getHouseholdRegisterNumber());
            entity.setHouseHoldTypeCode(dto.getHouseHoldTypeCode());
            entity.setIdentNumber(dto.getIdentNumber());
            entity.setIdentTypeCode(dto.getIdentTypeCode());
            entity.setIsMaster(dto.getMaster());
            entity.setName(dto.getName());
            entity.setNameZw(dto.getNameZw());
            entity.setNationCode(dto.getNationCode());
            entity.setOrgAuditorId(entity.getOrgAuditorId());
            entity.setOrgAuditTime(DateUtil.parse(dto.getOrgAuditTime(), DateUtil.DEFAULT_DATE_FORMAT));
            entity.setOrgCode(dto.getOrgCode());
            entity.setPayCycleCode(dto.getPayCycleCode());
            entity.setPayStandardAmt(dto.getPayStandardAmt());
            entity.setProjectId(dto.getProjectId());
            entity.setRelationCode(dto.getRelationCode());
            entity.setState(dto.getState());
            entity.setSubsidyCount(dto.getSubsidyCount());
            entity.setSubsidyMoney(dto.getSubsidyMoney());
            entity.setSubsidyNum(dto.getSubsidyNum());
            entity.setSummaryId(dto.getSummaryId());
            entity.setTelephone(dto.getTelephone());
            entity.setTotalFormula(dto.getTotalFormula());
            entity.setTownAuditor(dto.getTownAuditor());
            entity.setTownAuditTime(DateUtil.parse(dto.getTownAuditTime(), DateUtil.DEFAULT_DATE_FORMAT));
            entity.setTownRegisterTime(DateUtil.parse(dto.getTownRegisterTime(), DateUtil.DEFAULT_DATE_FORMAT));
            entity.setTownReigsterId(dto.getTownReigsterId());
            entity.setUploadId(dto.getUploadId());
            entity.setYear(dto.getYear());
            entity.setAge(dto.getAge());

            entity.setDisabled(dto.getDisabled());
            if (UserUtils.getUser() != null) {
                entity.setCreator(UserUtils.getUser().getUserName());
            }
            entity.setCreateTime(DateUtil.getCurrentTime());
            entity.setModifyTime(DateUtil.getCurrentTime());
            registerSuccessMapperExt.insertSelective(entity);
        }
//        dto = RegisterSuccessDomainConvertUtils.convert(entity);
        return getCitizen(entity.getRegisterId());
    }


    //所有登记的居民信息，某批次
    public Pagination<RegisterSuccessDTO> listCitizen(RegisterSuccessSearchDTO searchDTO) {

        Pagination<RegisterSuccessDTO> result = new Pagination<RegisterSuccessDTO>(searchDTO.getCurrentPage(), searchDTO.getPerPage());
        RegisterSuccessExample example = buildSearchExample(searchDTO);
        int total = registerSuccessMapperExt.countByExample(example);
        Map<Integer, String> projectNameMap = new HashMap<>();
        Map<Integer, String> areaNameMap = new HashMap<>();
        Map<String, String> bankNameMap = new HashMap<>();
        result.setTotalCount(total);
        if (total > 0) {
            List<RegisterSuccessDTO> data = new ArrayList<RegisterSuccessDTO>();
            List<RegisterSuccess> list = registerSuccessMapperExt.selectByExampleWithPaging(example, result.getOffset(), result.getPageSize());
            for (RegisterSuccess entity : list) {
                RegisterSuccessDTO dto = RegisterSuccessDomainConvertUtils.convert(entity);

                if (projectNameMap.containsKey(dto.getProjectId())) {
                    dto.setProjectName(projectNameMap.get(dto.getProjectId()));
                } else {
                    dto.setProjectName(projectMapperExt.selectByPrimaryKey(dto.getProjectId()).getProjectName());
                    projectNameMap.put(dto.getProjectId(), dto.getProjectName());
                }


                if (areaNameMap.containsKey(entity.getAreaId())) {
                    dto.setAreaName(areaNameMap.get(entity.getAreaId()));
                } else {
                    dto.setAreaName(areaMapperExt.selectByPrimaryKey(entity.getAreaId()).getAreaName());
                    areaNameMap.put(entity.getAreaId(), dto.getAreaName());
                }

                if (StringUtils.isNotBlank(entity.getBankNumber()))
                    if (bankNameMap.containsKey(entity.getBankCode())) {
                        dto.setBankName(bankNameMap.get(entity.getBankCode()));
                    } else {
                        DictionaryDTO dictionaryDTO = (DictionaryDTO) cacheService.getCache(String.format(CommonConstant.KEY_DIC, CommonConstant.DIC_TYPE_BANKCODE, entity.getBankCode()));
                        dto.setBankName(dictionaryDTO.getDictionaryName());
                        bankNameMap.put(entity.getBankCode(), dictionaryDTO.getDictionaryName());
                    }


                List<ProjectExtensionDetailDTO> projectExtensionDetailDTOs = projectExtensionDetailManager.getExtDetailByRegistId(entity.getRegisterId());
                if (projectExtensionDetailDTOs != null && projectExtensionDetailDTOs.size() > 0) {
                    dto.setProjectExtensionDetailList(projectExtensionDetailDTOs);
                }

                data.add(dto);
            }
            result.setList(data);
        }
        return result;
    }

    //获取某个已经成功登记的居民信息
    public RegisterSuccessDTO getCitizen(Integer registerId) {
        RegisterSuccessDTO dto = RegisterSuccessDomainConvertUtils.convert(registerSuccessMapperExt.selectByPrimaryKey(registerId));

        dto.setProjectExtensionDetailList(projectExtensionDetailManager.getExtDetailByRegistId(registerId));
        dto.setAreaName(areaMapperExt.selectByPrimaryKey(dto.getAreaId()).getAreaName());

        return dto;
    }

    //保存某个登记成功人员信息
    public RegisterSuccessDTO saveCitizen(RegisterSuccessDTO dto) {

        //登记成功记录表只允许修改登记金额
        RegisterSuccess registerSuccess = registerSuccessMapperExt.selectByPrimaryKey(dto.getRegisterId());
        registerSuccess.setSubsidyMoney(dto.getSubsidyMoney());
        //registerSuccess.setState(ProjectRegisterEn.register_ing.getCode());
        registerSuccessMapperExt.updateByPrimaryKeySelective(registerSuccess);

        //修改登记成功扩展属性表
        for (ProjectExtensionDetailDTO ped : dto.getProjectExtensionDetailList()) {
            projectExtensionDetailManager.save(ped);
        }
        return getCitizen(dto.getRegisterId());
    }

    //删除某个已经成功登记居民
    public Boolean deleteCitizen(RegisterDeleteDTO dto) {
        Boolean ret = true;
        if (dto.getRegisterIds() != null && dto.getRegisterIds().size() > 0) {
            for (Integer registerId : dto.getRegisterIds()) {
                RegisterSuccess entity = registerSuccessMapperExt.selectByPrimaryKey(registerId);
                entity.setIsDelete(true);
                entity.setModifyTime(DateUtil.getCurrentDate());
                registerSuccessMapperExt.updateByPrimaryKeySelective(entity);
            }
        }
        return ret;
    }


    //乡镇登记复核
    //*****************************************
    //待审核的批次下的所有登记记录列表
    public Pagination<RegisterSuccessDTO> townsAuditorWaitCizList(RegisterSuccessSearchDTO searchDTO) {
        return listCitizen(searchDTO);
    }


    /*主管部门审批*/
    //+++++++++++++++++++++++++++++++++++++++++++++++
    //获取某个批次汇总下的登记成功人员记录
    public Pagination<RegisterSuccessDTO> orgAuditorRegisterList(RegisterSuccessSearchDTO searchDTO) {

        List<Integer> state = new ArrayList<>();
        state.add(ProjectRegisterEn.org_auditor_ing.getCode());
        state.add(ProjectRegisterEn.org_auditor_ed.getCode());
        state.add(ProjectRegisterEn.org_auditor_fail.getCode());
        state.add(ProjectRegisterEn.finance_auditor_ing.getCode());
        state.add(ProjectRegisterEn.finance_auditor_ed.getCode());
        state.add(ProjectRegisterEn.finance_auditor_fail.getCode());
        state.add(ProjectRegisterEn.finance_created_file.getCode());
        state.add(ProjectRegisterEn.finance_downloaded_file.getCode());
        state.add(ProjectRegisterEn.bank_success.getCode());
        state.add(ProjectRegisterEn.bank_fail.getCode());
        searchDTO.setState(state);

        return listCitizen(searchDTO);
    }


    //查看某批次汇总下的登记成功人员记录
    public Pagination<RegisterSuccessDTO> financeAuditorRegisterList(RegisterSuccessSearchDTO searchDTO) {

        List<Integer> state = new ArrayList<>();

        state.add(ProjectRegisterEn.finance_auditor_ing.getCode());
        state.add(ProjectRegisterEn.finance_auditor_ed.getCode());
        state.add(ProjectRegisterEn.finance_auditor_fail.getCode());
        state.add(ProjectRegisterEn.finance_created_file.getCode());
        state.add(ProjectRegisterEn.finance_downloaded_file.getCode());
        state.add(ProjectRegisterEn.bank_success.getCode());
        state.add(ProjectRegisterEn.bank_fail.getCode());
        searchDTO.setState(state);

        return listCitizen(searchDTO);

    }


    @Autowired
    private DictionaryManager dictionaryManager;

    /**
     * 根据条件查询上报成功的居民信息,移动版
     *
     * @param searchDTO
     * @return
     */
    public List<RegisterSuccessDTO> getRegisterSuccessList(RegisterSuccessSearchDTO searchDTO) {
        RegisterSuccessExample example = new RegisterSuccessExample();
        RegisterSuccessExample.Criteria criteria = example.createCriteria();

        if (searchDTO.getYear() != null && searchDTO.getYear() > 0) {
            criteria.andYearEqualTo(searchDTO.getYear());
        }

        if (searchDTO.getProjectId() != null && searchDTO.getProjectId() > 0) {
            criteria.andProjectIdEqualTo(searchDTO.getProjectId());
        }
        if (searchDTO.getSubsidyCount() != null && searchDTO.getSubsidyCount() > 0) {
            criteria.andSubsidyCountEqualTo(searchDTO.getSubsidyCount());
        }
        if (searchDTO.getAreaId() != null && searchDTO.getAreaId() > 0) {
            criteria.andAreaIdEqualTo(searchDTO.getAreaId());
        }
        /*if (StringUtils.isNotBlank(searchDTO.getName())) {
            criteria.andNameEqualTo(searchDTO.getName());
        }*/

        if (StringUtils.isNotBlank(searchDTO.getIdentNumber())) {
            criteria.andIdentNumberEqualTo(searchDTO.getIdentNumber());
            //projectIds.add();
        }
        if (StringUtils.isNotBlank(searchDTO.getSummaryId())) {
            criteria.andSummaryIdEqualTo(searchDTO.getSummaryId());
        }

        criteria.andIsDeleteEqualTo(Boolean.FALSE);
        if (StringUtils.isNotBlank(searchDTO.getOrderByClause())) {
            example.setOrderByClause(searchDTO.getOrderByClause());
        } else {
            example.setOrderByClause("register_id asc");
        }

        List<RegisterSuccess> registerSuccessList = registerSuccessMapperExt.selectByExample(example);
        List<RegisterSuccessDTO> data = new ArrayList<RegisterSuccessDTO>();
        for (RegisterSuccess entity : registerSuccessList) {
            RegisterSuccessDTO dto = RegisterSuccessDomainConvertUtils.convert(entity);
            Area area = (Area) cacheService.getCache(String.format(CommonConstant.KEY_AREA_DATA, entity.getAreaId()));
            if (area != null)
                dto.setAreaName(area.getAreaName());
            else
                dto.setAreaName(areaMapperExt.selectByPrimaryKey(entity.getAreaId()).getAreaName());
            ProjectRegisterBatch projectRegisterBatch = projectRegisterBatchMapperExt.selectByPrimaryKey(entity.getBatchId());
            dto.setProjectName(projectRegisterBatch.getProjectName());
//            dto.setPayCycleCode(entity.getPayCycleCode());
//            dto.setSubsidyCount(entity.getSubsidyCount());
            dto.setStateName(ProjectRegisterEn.getMean(entity.getState()));
            try {
                logger.info("hhhhhh: " + JSONObject.fromObject(entity).toString());
                DictionaryDTO dictionaryDTO = dictionaryManager.getDicByCodeAndTypeId(entity.getBankCode(), CommonConstant.DIC_TYPE_BANKCODE);
                if (dictionaryDTO != null)
                    dto.setBankName(dictionaryDTO.getDictionaryName());
//                DictionaryDTO dictionaryDTO = (DictionaryDTO) cacheService.getCache(String.format(CommonConstant.KEY_DIC, CommonConstant.DIC_TYPE_PAYCYCLE, entity.getPayCycleCode()));
//                dto.setPayCycleName(dictionaryDTO.getDictionaryName());
                dictionaryDTO = dictionaryManager.getDicByCodeAndTypeId(entity.getPayCycleCode(), CommonConstant.DIC_TYPE_PAYCYCLE);
                if (dictionaryDTO != null)
                    dto.setPayCycleName(dictionaryDTO.getDictionaryName());

            } catch (Exception ex) {
                logger.error(ex.getMessage());
                ex.printStackTrace();
            }

            data.add(dto);
        }
        return data;
    }


    public List<RegisterSuccessDTO> getRegisterSuccessListForCreateFile(String summaryId, List<Integer> states) {
        RegisterSuccessExample example = new RegisterSuccessExample();
        RegisterSuccessExample.Criteria criteria = example.createCriteria();

        criteria.andSummaryIdEqualTo(summaryId);
        criteria.andStateIn(states);

        criteria.andIsDeleteEqualTo(Boolean.FALSE);

        List<RegisterSuccess> registerSuccessList = registerSuccessMapperExt.selectByExample(example);
        List<RegisterSuccessDTO> data = new ArrayList<RegisterSuccessDTO>();
        for (RegisterSuccess entity : registerSuccessList) {
            RegisterSuccessDTO dto = RegisterSuccessDomainConvertUtils.convert(entity);
            data.add(dto);
        }
        return data;
    }


    //build查询Example
    private RegisterSuccessExample buildSearchExample(RegisterSuccessSearchDTO search) {
        RegisterSuccessExample example = new RegisterSuccessExample();
        RegisterSuccessExample.Criteria criteria = example.createCriteria();

        if (search.getBatchId() != null && search.getBatchId() > 0) {
            criteria.andBatchIdEqualTo(search.getBatchId());
        }
        if (StringUtils.isNotBlank(search.getIdentNumber())) {
            criteria.andIdentNumberEqualTo(search.getIdentNumber());
        }
        if (StringUtils.isNotBlank(search.getName())) {
            criteria.andNameEqualTo(search.getName());
        }
        if (search.getState() != null && search.getState().size() > 0) {
            criteria.andStateIn(search.getState());
        }
        if (search.getAreaId() != null && search.getAreaId() > 0) {

            List<Integer> ids = (List<Integer>) cacheService.getCache(String.format(CommonConstant.KEY_AREA, search.getAreaId()));

            if (ids != null && ids.size() > 0) {
                ids.add(search.getAreaId());
                criteria.andAreaIdIn(ids);
            } else {
                criteria.andAreaIdEqualTo(search.getAreaId());
            }
        }

        if (search.getProjectCategoryId() != null && search.getProjectCategoryId() > 0 && search.getProjectId() == null) {
            //如果项目分类不为空，且projectId为空
            List<Integer> projectIds = projectManager.getProjectIdsByCategoryId(search.getProjectCategoryId());
            if (projectIds != null && projectIds.size() > 0)
                criteria.andProjectIdIn(projectIds);
        }

        if (search.getProjectId() != null && search.getProjectId() > 0) {
            criteria.andProjectIdEqualTo(search.getProjectId());
        }

        if (search.getYear() != null && search.getYear() > 0) {
            criteria.andYearEqualTo(search.getYear());
        }
        if (search.getSubsidyCount() != null && search.getSubsidyCount() > 0) {
            criteria.andSubsidyCountEqualTo(search.getSubsidyCount());
        }

        if (StringUtils.isNotBlank(search.getPayCycleCode())) {
            criteria.andPayCycleCodeEqualTo(search.getPayCycleCode());
        }

        if (StringUtils.isNotBlank(search.getSummaryId())) {
            criteria.andSummaryIdEqualTo(search.getSummaryId());
        }

        criteria.andIsDeleteEqualTo(Boolean.FALSE);
        if (StringUtils.isNotBlank(search.getOrderByClause())) {
            example.setOrderByClause(search.getOrderByClause());
        } else {
            example.setOrderByClause("register_id asc");
        }
        return example;
    }

    //如果上传银行回执执行完毕，则调用改方法，修改相关记录状态
    public void bankReturnFinish(String summaryId, BarDTO bar) {

        //更新 pay_summary
        List<PaySummaryDTO> paySummaryList = paySummaryManager.all(summaryId);
        if (paySummaryList != null && paySummaryList.size() > 0) {
            for (PaySummaryDTO dto : paySummaryList) {
                if (UserUtils.getUser() != null && UserUtils.getUser().getUserId() > 0) {
                    dto.setRecUploader(UserUtils.getUser().getUserName());
                    dto.setRecUploadTime(DateUtil.getCurrentTime());
                    paySummaryManager.save(dto);
                }
            }
        }

        /*更新状态*/
        Integer bankCallBackState = 0;
        if (bar.getSuccess() > 0) {
            if (bar.getFail() > 0) {
                bankCallBackState = ProjectRegisterBatchEn.bank_success_part.getCode();
            } else {
                bankCallBackState = ProjectRegisterBatchEn.bank_success_all.getCode();
            }
        } else {
            bankCallBackState = ProjectRegisterBatchEn.bank_fail_all.getCode();
        }

        //更新 org_batch_summary

        OrgBatchSummaryDTO orgBatchSummaryDTO = orgBatchSummaryManager.get(summaryId);
        orgBatchSummaryDTO.setState(bankCallBackState);
        orgBatchSummaryManager.save(orgBatchSummaryDTO);

        //更新 project_register_batch
        ProjectRegisterBatch update = new ProjectRegisterBatch();
        ProjectRegisterBatchExample example = new ProjectRegisterBatchExample();
        ProjectRegisterBatchExample.Criteria criteria = example.createCriteria();
        criteria.andBatchIdIn(orgBatchSummaryManager.selectBatchIdsBySummaryId(summaryId));
        criteria.andIsDeleteEqualTo(false);
        update.setState(bankCallBackState);
        update.setModifyTime(DateUtil.getCurrentTime());
        projectRegisterBatchMapperExt.updateByExampleSelective(update, example);

    }

}
